import {playMode} from 'common/js/config'
import {shuffle} from 'common/js/util'
import {saveSearch, clearSearch, deleteSearch, savePlay, saveFavorite, deleteFavorite} from 'common/js/cache'

function findIndex(list, song) { //查找当前列表中 是否有这一条数据
  return list.findIndex((item) => {
    return item.id === song.id
  })
}

//选择播放 music-list
export const selectPlay = function ({commit, state}, {list, index}) {
	commit('SET_SEQUENCE_LIST',list)//顺序列表
	if(state.mode==playMode.random){ //已经播放了 在点击随机播放时的处理
		let randomList = shuffle(list)
		commit('SET_PLAYLIST',randomList)//播发列表
		index=findIndex(randomList,randomList[index])
	}else{
		commit('SET_PLAYLIST',list)//播发列表
	}
	commit('SET_CURRENT_INDEX',index)//当前播放的索引
	commit('SET_FULL_SCREEN',true)//播放器张开还是收起
	commit('SET_PLAYING_STATE',true)//播放状态
	commit('CURRENTSONG',list[index])//当前播放歌曲
	// console.log("com",list,index)
}

//选择上一首 下一首
export const NexTsell = function ({commit, state}, {list, index}) {
	commit('SET_CURRENT_INDEX',index)//当前播放的索引
	commit('CURRENTSONG',list[index])//当前播放歌曲
  // console.log("naet",list,index,list[index])
}

//随机播发黄色的文字 music-list
export const randomPlay = function ({commit}, {list}) {
  commit('SET_PLAY_MODE', playMode.random) //播放模式
	commit('SET_SEQUENCE_LIST',list)//顺序列表
	let randomList = shuffle(list)
	commit('SET_PLAYLIST', randomList)//播放状态
	commit('SET_CURRENT_INDEX', 0)//播放状态
	commit('SET_FULL_SCREEN', true)//播放器张开还是收起
	commit('SET_PLAYING_STATE', true)//播放状态
	commit('CURRENTSONG',list[0])//当前播放歌曲
}

//排行榜搜索点击音乐
export const insertSong = function ({commit, state},song) {
	let playlist = state.playlist.slice()
	let sequenceList = state.sequenceList.slice()
	let currentIndex = state.currentIndex
	// 记录当前歌曲
	let currentSong = playlist[currentIndex]
	// 查找当前列表中是否有待插入的歌曲并返回其索引
	let fpIndex = findIndex(playlist,song)
	// 因为是插入歌曲，所以索引+1
  currentIndex++
  // 插入这首歌到当前索引位置
  playlist.splice(currentIndex, 0,song)
	  // 如果已经包含了这首歌
  if (fpIndex > -1) {
	// 如果当前插入的序号大于列表中的序号
		if (currentIndex > fpIndex) {
			playlist.splice(fpIndex, 1)
			currentIndex--
		} else {
			playlist.splice(fpIndex + 1, 1)
		}
  }
	//sequenceList播放模式的歌曲
	//当前歌曲 在播放列表里面查找 index
	let  currentSIndex=findIndex(sequenceList,currentSong)+1
	let	 fsindex=findIndex(sequenceList,song)
	sequenceList.splice(currentSIndex,0,song)
	if (fsindex>-1) {
		if (currentSIndex > fpIndex) {
			sequenceList.splice(fpIndex, 1)
		} else {
			sequenceList.splice(fpIndex + 1, 1)
		}
	}

	commit("SET_PLAYLIST", playlist)
	commit("SET_SEQUENCE_LIST", sequenceList)
	commit("SET_CURRENT_INDEX", currentIndex)
	commit("SET_FULL_SCREEN", true)
	commit("SET_PLAYING_STATE", true)
	commit('CURRENTSONG',song)//当前播放歌曲
	
}

//搜索历史
export const saveSearchHistory = function ({commit}, query) {
  commit("SET_SEARCH_HISTORY", saveSearch(query))
}

//删除的通用方法
export const deleteSearchHistory = function ({commit}, query) {
  commit("SET_SEARCH_HISTORY", deleteSearch(query))
}

//点击删除按钮全部清空数据[]
export const clearSearchHistory = function ({commit}) {
  commit("SET_SEARCH_HISTORY", clearSearch())
}

//底部顺序列表点击删除
export const deleteSong = function ({commit, state}, song) {
  let playlist = state.playlist.slice()
  let sequenceList = state.sequenceList.slice()
  let currentIndex = state.currentIndex
	
  let pIndex = findIndex(playlist, song)
  playlist.splice(pIndex, 1)
  let sIndex = findIndex(sequenceList, song)
  sequenceList.splice(sIndex, 1)
	
	//当前下标大于查找的下标 歌曲等于==列表length、
  if (currentIndex > pIndex || currentIndex === playlist.length) {
    currentIndex--
  }

  commit("SET_PLAYLIST", playlist)
  commit("SET_SEQUENCE_LIST", sequenceList)
  commit("SET_CURRENT_INDEX", currentIndex)
	commit('CURRENTSONG',playlist[currentIndex])//当前播放歌曲
	const playingState=playlist.length>0
	commit("SET_PLAYING_STATE", playingState)
	
}

//playlist  全部删除
export const deleteSongList = function ({commit}) {
  commit("SET_CURRENT_INDEX", -1)
  commit("SET_PLAYLIST", [])
  commit("SET_SEQUENCE_CLEAR", [])
  commit("SET_PLAYING_STATE", false)
}

//播放器 写入缓冲
export const savePlayHistory = function ({commit}, song) {
  commit("SET_PLAY_HISTORY", savePlay(song))
}

//用户喜欢的数据 add
export const saveFavoriteList = function ({commit}, song) {
  commit("SET_FAVORITE_LIST", saveFavorite(song))
}
//用户喜欢的数据 clear
export const deleteFavoriteList = function ({commit}, song) {
  commit("SET_FAVORITE_LIST", deleteFavorite(song))
}